<?php
namespace Sample\CaptureIntentExamples;

require __DIR__ . '/../../vendor/autoload.php';

use Sample\PayPalClient;
use SyPay\PayPal\Orders\OrdersCaptureRequest;

class CaptureOrder
{
    /**
     * This function can be used to capture an order payment by passing the approved
     * order id as argument.
     *
     * @param $orderId
     * @param bool $debug
     *
     * @return mixed
     */
    public static function captureOrder($orderId, $debug = false)
    {
        $request = new OrdersCaptureRequest($orderId);

        $client = PayPalClient::client();
        $response = $client->execute($request);
        if ($debug) {
            echo "Status Code: {$response->statusCode}\n";
            echo "Status: {$response->result->status}\n";
            echo "Order ID: {$response->result->id}\n";
            echo "Links:\n";
            foreach ($response->result->links as $link) {
                echo "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
            }
            echo "Capture Ids:\n";
            foreach ($response->result->purchase_units as $purchase_unit) {
                foreach ($purchase_unit->payments->captures as $capture) {
                    echo "\t{$capture->id}";
                }
            }
            // To toggle printing the whole response body comment/uncomment below line
            echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
        }

        return $response;
    }
}

/**
 * This is the driver function which invokes the captureOrder function with
 * <b>Approved</b> Order Id to capture the order payment.
 */
if (!count(debug_backtrace())) {
    CaptureOrder::captureOrder('0F105083N67049335', true);
}
